import { _decorator, Component, Node, SpriteFrame } from 'cc';
import { BasePanel } from './BasePanel';
import { SignItemSub } from './items/SignItemSub';
import { _item, _platform, _player } from '../Main';
import { SignItem } from './items/SignItem';
import { TT } from '../platforms/minis/TT';
import { Adsdk } from '../scenes/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('SignPanel')
export class SignPanel extends BasePanel {
    
    panelName: string = '七日签到'

    @property([SignItem])
    signItems: SignItem[] = []

    @property([SpriteFrame])
    bottoms: SpriteFrame[] = []

    @property(Node)
    shareNode: Node = null!

    @property(Node)
    videoNode: Node = null!

    @property(Node)
    gotNode: Node = null!

    start(): void {
        this.init()
        this.show()
        Adsdk.instance.showInterstitialAd()
    }

    init() {
        this.initItems()
        this.refreshButtons()
        this.refreshItems()
    }

    initItems() {
        this.signItems[0].init(1, [{ item: _item.items.Coin, amount: 10 }])
        this.signItems[1].init(2, [{ item: _item.items.MDClear, amount: 1 }])
        this.signItems[2].init(3, [{ item: _item.items.Coin, amount: 20 }])
        this.signItems[3].init(4, [{ item: _item.items.MDUncover, amount: 1 }])
        this.signItems[4].init(5, [{ item: _item.items.Coin, amount: 30 }])
        this.signItems[5].init(6, [{ item: _item.items.MDRefresh, amount: 1 }])
        this.signItems[6].init(7, [{ item: _item.items.Coin, amount: 50 }, { item: _item.items.MDClear, amount: 1 }, { item: _item.items.MDRefresh, amount: 1 }])
    }

    refreshButtons() {
        let b: boolean = _player.daily.sign > 0
        this.shareNode.active = b
        this.videoNode.active = b
        this.gotNode.active = !b
    }

    refreshItems() {
        for (let i = 0; i < this.signItems.length; i++) {
            let signItem: SignItem = this.signItems[i]
            let n: number = i + 1
            let signed: boolean = _player.data.signDay > n || (_player.data.signDay == n && _player.daily.sign <= 0)
            signItem.refreshState(signed, this.bottoms)
        }
    }

    getBySignDay(multi: number = 1) {
        let b: boolean = _player.daily.sign > 0
        if (!b) {

            return
        }
        _player.changeNumData('sign', _player.daily, -1, 'add', true, () => {
            let day: number = _player.data.signDay
            let signItem: SignItem = this.signItems[day - 1]
            signItem.get(multi)
            this.refreshButtons()
            this.refreshItems()
            let pf = _platform.platform
            if (pf instanceof TT) {
                pf.reportAnalytics('Behavior', {
                    purpose: '获取',
                    type: '奖励',
                    name: '签到',
                    amount: day,
                    source: this.panelName + '-' + (multi == 1 ? '分享' : '广告'),
                })
            }
        })
    }

    share() {
        _platform.platform.share((success) => {
            if (success) {
                this.getBySignDay(1)
            } else {

            }
        }, 'invite')
    }

    video() {
        _platform.platform.showVideo(() => {
            this.getBySignDay(2)
        }, () => {
            
        })
    }

    got() {
        this.close()
    }
}


